﻿using StudySystem.MainLogic;
using StudySystem.MainLogic.Items;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudySystem.SQLServer
{
    public static class DBForQuestion
    {
        /// <summary>
        /// 获得当前用户所有的问题
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static SqlDataAdapter GetQuestion(ref DataSet ds)
        {
            string sql = "select SubjectID,QuestionID,QuestionContent,Answer " +
                "from QuestionTable where UserID = " + UserManagement.user.UserID;
            return DbHelper.GetTable(sql, ref ds, "questionTable");
        }


        public static int InsertAQuestion(string subjectID, string question, string answer)
        {
            string sql = "insert into QuestionTable(UserID,SubjectID,QuestionContent,Answer) " +
                "values(@userID,@subjectID,@quesContent,@answer)";
            SqlParameter paraUserID = new SqlParameter("@userID", SqlDbType.NChar);
            paraUserID.Value = UserManagement.user.UserID;
            SqlParameter paraSubID = new SqlParameter("@subjectID", SqlDbType.Int);
            paraSubID.Value = subjectID;
            SqlParameter paraQues = new SqlParameter("@quesContent", SqlDbType.NVarChar);
            paraQues.Value = question;
            SqlParameter paraAnswer = new SqlParameter("@answer", SqlDbType.NVarChar);
            paraAnswer.Value = answer;
            return DbHelper.SafeExecuteNonQuery(sql, paraUserID, paraSubID, paraQues, paraAnswer);
        }

        public static int DeleteAQuestion(string questionID)
        {
            string sql = "delete from RecordTable where QuestionID = " + questionID + ";" +
               "delete from QuestionTable where QuestionID = " + questionID + "; "; 
            return DbHelper.ModifyRecord(sql);
        }

        public static int ModifyAQuestion(string questionID, string newQuestion, string newAnswer)
        {
                string sql = "update QuestionTable set QuestionContent = @paraQues, " +
                    "Answer = @paraAnswer where QuestionID = @paraQuesID";
                SqlParameter paraQues = new SqlParameter("@paraQues", SqlDbType.NVarChar);
                paraQues.Value = newQuestion;
                SqlParameter paraAnswer = new SqlParameter("@paraAnswer", SqlDbType.NVarChar);
                paraAnswer.Value = newAnswer;
                SqlParameter paraQuesID = new SqlParameter("@paraQuesID", SqlDbType.Int);
                paraQuesID.Value = questionID;
                return DbHelper.SafeExecuteNonQuery(sql, paraQues, paraAnswer, paraQuesID);
        }

        

    }
}
